package com.suda.sudaexamsystem.service.impl;

import com.suda.sudaexamsystem.mapper.QuestionMapper;
import com.suda.sudaexamsystem.pojo.Question;
import com.suda.sudaexamsystem.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Service
public class QuestionServiceImpl implements QuestionService {
    @Autowired
    private QuestionMapper questionMapper;

    @Override
    @Transactional
    public void addQuestion(Question question) {
        questionMapper.addQuestion(question);
    }

    @Override
    @Transactional
    public void addQuestionBatch(List<Question> questionList) {
        questionMapper.addQuestionBatch(questionList);
    }

    @Override
    @Transactional
    public void updateQuestion(Question question) {
        questionMapper.updateQuestion(question);
    }

    @Override
    @Transactional
    public void deleteById(int questionId) {
        questionMapper.deleteById(questionId);
    }

    @Override
    @Transactional
    public void deleteByExamId(int examId) {
        questionMapper.deleteByExamId(examId);
    }

    @Override
    @Transactional
    public void deleteByIdBatch(String questionIds) {
        questionMapper.deleteByIdBatch(questionIds);
    }

    @Override
    @Transactional
    public Question findById(int questionId) {
        return questionMapper.findById(questionId);
    }

    @Override
    @Transactional
    public List<Question> findByExamId(int examId) {
        List<Question> questionList = questionMapper.findByExamId(examId);
        Collections.sort(questionList, Comparator.comparingInt(Question::getOrderId));
        return questionList;
    }
}
